<template>
  <div class="app-main" v-loading="loading">
    <div class="divTmp" v-show="loading"></div>
    <div class="index-body" v-show="!loading">
      <div class="ko-view">
        <div class="form-wrapper">
          <el-form name="form"
                   class="updatePas-form form"
                   :model="form"
                   ref="form"
                   :rules="rules">
            <div class="form-group clearfix">
              <div class="user-info-label">
                <i> * </i>
                <span>旧密码</span>
              </div>
              <el-form-item prop="original">
                <el-input class="text-input" type="password" v-model="form.original"/>
              </el-form-item>

            </div>
            <div class="form-group clearfix">
              <div class="user-info-label">
                <i> * </i>
                <span>新密码</span>

              </div>
              <el-form-item prop="password">
                <el-input class="text-input" type="password" v-model="form.password"/>
              </el-form-item>

              <!--            实际密码为5-8为随机字符，默认密码为123456-->
              <label class="bottom-tips">8-16位，须包含至少3种字符（数字、大写字母、小写字母或特殊符号）</label>
            </div>
            <div class="form-group clearfix" style="margin-top: 40px">
              <div class="user-info-label">
                <i> * </i>
                <span>确认密码</span>
              </div>
              <el-form-item prop="repassword">
                <el-input class="text-input" type="password" v-model="form.repassword"/>
              </el-form-item>

            </div>


            <div class="button-group clearfix">
              <button type="submit" class="button button-red-solid pull-left"
                      @click="save('form')">确定
              </button>
              <button type="button" class="button button-red-hollow pull-right"
                      @click="cancelForm">取消
              </button>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {updatePassword} from "@/apis/people";


export default {
  name: "password",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        // if (this.ruleForm.checkPass !== "") {
        //   this.$refs.ruleForm.validateField("checkPass");
        // }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      loading: true,
      form: {
        original: '',
        password: '',
        repassword: '',
      },
      rules: {
        original: [
          {required: true, message: "输入框不能为空", trigger: "blur"},
        ],
        password: [
          {required: true, message: "输入框不能为空", trigger: "blur"},
          {validator: validatePass, trigger: "blur"},

          {
            min: 5,
            max: 8,
            message: "长度在 5 到 8 个任意字符",
            trigger: "blur",
          },
        ],
        repassword: [
          {required: true, message: "输入框不能为空", trigger: "blur"},
          {validator: validatePass2, trigger: "blur"},

          {
            min: 5,
            max: 8,
            message: "长度在 5 到 8 个任意字符",
            trigger: "blur",
          },
        ],
      }
    }
  },
  created() {


    this.init()
  },

  methods: {
    // 资源初始化
    async init() {

      this.form = {}
      this.closeLoading()
    },

    // 提交表单
    save(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let user = {
            id: localStorage.getItem("id"),
            password: this.form.password,
            original: this.form.original,
          }
          if (this.form.password === this.form.original) {
            this.form = {}
            this.$message.error("新密码不能与旧密码相同")
          } else {
            updatePassword(user).then((resp) => {
              if (resp?.data?.code == 1) {
                this.form = {}
                this.$message.success("修改密码成功!!")
              } else {
                this.$message.error(resp?.data?.msg)
                this.form = {}
              }
            })
          }


        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 加载动画
    closeLoading() {
      this.timer = null
      this.timer = setTimeout(() => {
        this.loading = false
      }, 500)
    },

    // 取消个人信息页面
    cancelForm() {
      let type = localStorage.getItem("type")
      if (type === "student") {
        this.$router.push({
          name: 'studentIndex'
        }).catch(err => {
          console.log(err)
        })
      }
      if (type === "teacher") {
        this.$router.push({
          name: 'teacherIndex',
        }).catch(err => {
          console.log(err)
        })
      }
      if (type === "admin") {
        this.$router.push({
          name: 'adminIndex',
        }).catch(err => {
          console.log(err)
        })
      }

    },
  }

}
</script>

<style scoped>
*, :after, :before {
  box-sizing: border-box;
}
.divTmp {
  width: 100%;
}

::v-deep .el-loading-mask {
  transition: opacity 0s;
}

::v-deep .el-loading-spinner {
  top: 100%;
  margin-top: 30%;
  width: 100%;
  text-align: center;
  position: absolute;
}

div {
  display: block;
}

body, html {
  color: #444;
  font-size: 14px;
}

a {
  background-color: transparent;
  color: #337ab7;
  text-decoration: none;
}

a:active, a:hover {
  outline: 0;
}

.form-wrapper {
  margin: 30px auto;
  padding: 0 20px;
  max-width: 760px;
}

.form-group {
  margin-bottom: 26px;
  width: 100%;
  min-height: 32px;
  position: relative;
}


.updatePas-form {
  margin: 50px auto;
  width: 436px;
}


.user-info-label {
  margin-top: 3px;
  float: left;
  text-align: right;
  margin-right: 20px;
  width: 100px;
  height: 32px;
  line-height: 32px;
}

.user-info-label i {

  font-style: normal;
  font-weight: 700;
  color: #ea5947;
}

.pull-left {
  float: left !important;

}

form {
  display: block;
  margin-top: 0em;
}


::v-deep .el-input__inner {
  padding: 0 12px;
  border-radius: 4px;
  background-color: #fff;
  border: 1px solid #e3e3e9;
  width: 270px;
  height: 32px;
  font-size: 14px;
  line-height: 32px;
  outline: #969696;
}


.school {
  padding-left: 15px;
  color: rgba(173, 163, 162, 0.59);
}

.button-red-hollow {
  background-color: #fff;
  border-color: #e3e3e9;
  color: #7691fc;
}

.button-red-hollow:hover {
  border-color: #7691fc;
}

.button-red-solid {
  background-color: #7691fc;
  border-color: #7691fc;
}

.bottom-tips {
  margin-left: 123px;
  margin-top: 5px;
  left: 0;
  color: #969696;
  white-space: nowrap;
  position: absolute;
  width: 240px;
  bottom: -45px;
  font-size: 12px;
  margin-bottom: 5px;
  display: inline-block;
  max-width: 100%;
}

.button-group {
  margin: 10px 0 0 120px;
  height: 32px;
  width: 270px;
}

::v-deep .el-form-item__error {
  margin-left: 28%;
}

.pull-right {
  float: right !important;
  background-color: #fff;
  border-color: #e3e3e9;
}
</style>